package cn.zqh.commodity.controller;


import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.bo.*;
import cn.zqh.commodity.client.StoreClient;
import cn.zqh.commodity.service.CommodityService;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import feign.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.C;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zqh
 * @since 2022-12-24
 */
@RestController
@RequestMapping("/commodity")
@Api("commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StoreClient storeClient;

    /**
      * @Author: zqh
      * @Date:
      * @Description: 添加一个新商品
    */
    @PostMapping("/add")
    @ApiOperation(value = "添加一个新商品")
    public CommonResult addANewCommodity(@RequestBody Pfoods pfoods)throws IOException{
        return commodityService.addANewCommodity(pfoods);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询所有商品
    */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有商品")
    public CommonResult getAll(){
        return commodityService.getAll();
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 根据商品id删除商品
    */
    @ApiOperation(value = "根据商品id删除商品")
    @DeleteMapping("/delACommodityById")
    public CommonResult delACommodityById(String id)throws IOException{
        return commodityService.delACommodityById(id);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改商品信息（基于店主）
    */
    @ApiOperation(value = "店主修改商品信息")
    @PostMapping("StoreOwnerModifiesProductInformation")
    public CommonResult StoreOwnerModifiesProductInformation(@RequestBody UserChangeCommodity userChangeCommodity)throws IOException{
        return commodityService.StoreOwnerModifiesProductInformation(userChangeCommodity);
    }
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 修改商品信息基于管理员
    */
    @ApiOperation(value = "管理员修改商品信息")
    @PostMapping("ModifyTheProductInformationBasedOnTheAdministrator")
    public CommonResult ModifyTheProductInformationBasedOnTheAdministrator(@RequestBody AdminChangeCommodity adminChangeCommodity)throws IOException{
        return commodityService.ModifyTheProductInformationBasedOnTheAdministrator(adminChangeCommodity);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 创建commodity的es数据库
    */
    @ApiOperation(value = "创建commodity的es数据库")
    @PostMapping("CreateEsDataBaseOfCommodity")
    public CommonResult CreateEsDataBaseOfCommodity() throws IOException {
        return commodityService.CreateEsDataBaseOfCommodity();
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 删除commodity的es数据库
    */
    @ApiOperation(value = "删除commodity的es数据库")
    @DeleteMapping("DelEsDataBaseOfCommodity")
    public CommonResult DelEsDataBaseOfCommodity()throws IOException{
        return commodityService.DelEsDataBaseOfCommodity();
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 判断commodity的es数据库是否存在
    */
    @ApiOperation(value = "判断commodity的es数据库是否存在")
    @GetMapping("DetermineWhetherTheEsCommunityExists")
    public CommonResult DetermineWhetherTheEsCommunityExists()throws IOException{
        return commodityService.DetermineWhetherTheEsCommunityExists();
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 把数据库中所有数据上传到es数据库
    */
    @ApiOperation(value = "把数据库中所有商品数据上传到es数据库")
    @PostMapping("UploadAllDataInTheDatabaseToTheEsDatabase")
    public CommonResult UploadAllDataInTheDatabaseToTheEsDatabase()throws IOException {
        return commodityService.UploadAllDataInTheDatabaseToTheEsDatabase();
    }
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 搜索引擎(别人输入时给其提示)
    */
    @ApiOperation(value = "搜索时输入关键字给与其提示")
    @GetMapping("searchEngines")
    public CommonResult searchEngines(String t)throws IOException {
        return commodityService.searchEngines(t);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 多条件复合查询
    */
    @ApiOperation(value = "多条件复合查询")
    @PostMapping("multiConditionalCompoundQuery")
    public CommonResult multiConditionalCompoundQuery(@RequestBody SearchBean searchBean)throws IOException{
        return commodityService.CompoundQuery(searchBean);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 根据id查询商品
    */
    @ApiOperation(value = "根据id查询商品")
    @GetMapping("getCommodityById")
    public CommonResult getCommodityById(String id)throws IOException{
        return commodityService.getCommodityById(id);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过商品信息获取商品信息，不是返回es数据库的信息
    */
    @ApiOperation(value = "根据id查询商品（不是返回es数据库的信息）")
    @GetMapping("getfoodsById")
    public CommonResult gCommodity(String id){
        return commodityService.gCommodity(id);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取所有商品得数量
    */
    @ApiOperation(value = "获取所有商品得数量(放行)")
    @GetMapping("getAllCommodityNumbers")
    public CommonResult getAllCommodityNumbers(Integer del){
        return commodityService.getAllCommodityNumbers(del);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取被删除的商品
    */
    @ApiOperation(value = "获取被删除了的商品")
    @GetMapping("getDelCommoditys")
    public CommonResult getDelCommoditys(Integer page,Integer size){
        return commodityService.getDelCommoditys(page,size);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 把删除的商品取消删除
    */
    @ApiOperation(value = "把删除的商品取消删除")
    @PostMapping("cancelDeletionOfDeletedGoods")
    public CommonResult cancelDeletionOfDeletedGoods(String id)throws IOException{
        return commodityService.cancelDeletionOfDeletedGoods(id);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 更改商品库存
    */
    @ApiOperation(value = "更改商品库存(放行)")
    @PostMapping("changeItemInventory")
    public CommonResult changeItemInventory(String commodityId,Integer number)throws IOException{
        return commodityService.changeItemInventory(commodityId,number);
    }

    @GetMapping("/tset")
    @ApiOperation(value = "测试templast")
    public CommonResult res(){
        CommonResult res = restTemplate.getForObject("http://127.0.0.1:9801/commodity/getCommodityById?id="+"commodity8321304189355024384",CommonResult.class);
//        EsCommodity dydj  = JSON.parseObject((byte[]) res.getData(),EsCommodity.class);
//        System.out.println(dydj);

        System.out.println(res.getData().getClass());
        System.out.println("张启航狂空来流放到狂赛道");
        System.out.println(res.getCode());
        String s = JSON.toJSONString(res.getData());
        EsCommodity esCommodity = JSON.parseObject(s, EsCommodity.class);
        System.out.println(esCommodity);
        System.out.println(esCommodity.getClass());
        return CommonResult.success(esCommodity);
    }




}

